package com.useeinfo.oa.modules.base.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.security.entity.SystemUser;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @Auther: xudong.sun
 * @Date: 2018/11/30 10:37
 * @Description: 企业表
 */
@Entity
@Table(name = "enterprise")
public class Enterprise extends SuperClassEntity<Enterprise> {

    /**
     * 企业名称
     */
    private String enterpriseName;

    /**
     * 企业编号
     */
    private String code;

    /**
     * 组织左
     */
    private Integer lft;
    /**
     * 组织右
     */
    private Integer rgt;
    /**
     * 等级
     */
    private Integer lvl;

    /**
     * 上级企业
     */
    private Long parentId;

    /**
     * 所有上级id，使用逗号分隔
     */
    @Column(length = 500)
    private String parentIds;
    /**
     * 多对多应用
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "enterprise_2_application", joinColumns = @JoinColumn(name = "enterprise_id"), inverseJoinColumns = @JoinColumn(name = "application_id"))
    private Set<ApplicationModule> applicationModuleSet = new HashSet<>();
    /**
     * 多对多用户---企业管理者
     */
    @ManyToMany(mappedBy = "enterpriseSet")
    private Set<SystemUser> systemUserSet = new HashSet<>();

    /**
     * 企业创建人
     */
    @ManyToOne
    @JoinColumn(name = "systemUser_id")
    private SystemUser systemUser;


    public String getEnterpriseName() {
        return enterpriseName;
    }

    public void setEnterpriseName(String enterpriseName) {
        this.enterpriseName = enterpriseName;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    public Integer getLft() {
        return lft;
    }

    public void setLft(Integer lft) {
        this.lft = lft;
    }

    public Integer getRgt() {
        return rgt;
    }

    public void setRgt(Integer rgt) {
        this.rgt = rgt;
    }

    public Integer getLvl() {
        return lvl;
    }

    public void setLvl(Integer lvl) {
        this.lvl = lvl;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public Set<ApplicationModule> getApplicationModuleSet() {
        return applicationModuleSet;
    }

    public void setApplicationModuleSet(Set<ApplicationModule> applicationModuleSet) {
        this.applicationModuleSet = applicationModuleSet;
    }

    public Set<SystemUser> getSystemUserSet() {
        return systemUserSet;
    }

    public void setSystemUserSet(Set<SystemUser> systemUserSet) {
        this.systemUserSet = systemUserSet;
    }

    public void addApplicationModule(ApplicationModule applicationModule) {
        this.applicationModuleSet.add(applicationModule);
    }

    public void removeApplicationModule(ApplicationModule applicationModule) {
        this.applicationModuleSet.remove(applicationModule);
    }

    public SystemUser getSystemUser() {
        return systemUser;
    }

    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("enterpriseName", StringConverters.null2String(getEnterpriseName()));
        jsonObject.put("code", StringConverters.null2String(getCode()));
        jsonObject.put("createUser", getSystemUser()==null?"":StringConverters.null2String(getSystemUser().getUserName()));
        return jsonObject;
    }

    @Override
    public int hashCode() {
        return getId().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) return false;
        if(this == obj) return true;
        if(obj instanceof Enterprise){
            Enterprise enterprise = (Enterprise)obj;
            return enterprise.getId().equals(this.getId());
        }
        return false;
    }
}
